import requests
import re
import json
from bs4 import BeautifulSoup
import sqliteUtils
from multiprocessing.dummy import Pool as ThreadPool


def request(url):
    i = 0
    while i < 3:
        i = i + 1
        req = requests.get(url)
        if req.status_code == requests.codes.ok:
            req.encoding = "utf-8"
            return req.text
    return None


def getArtList():
    list = []
    html = request("http://music.taihe.com/artist")
    soup = BeautifulSoup(html, features="lxml")
    alist = soup.find_all(
        "a", href=re.compile("artist/[0-9]\\d*"), attrs={"class": ""})
    for art in alist:
        dict = {"name": "", "path": ""}
        dict["name"] = art.string
        dict["path"] = art["href"]
        list.append(dict)
    return list


def getMusicList(art):
    list = []
    url = "http://music.taihe.com" + art["path"]
    html = request(url)
    if html is None:
        print("musiclist is null : " + art["name"])
        return None
    soup = BeautifulSoup(html, features="lxml")
    mlist = soup.find_all(
        "a", href=re.compile("/song/[0-9]\\d*"), attrs={"class": "songlist-songname"})
    for music in mlist:
        dict = {"name": "", "path": ""}
        dict["name"] = music["title"]
        dict["path"] = music["href"]
        list.append(dict)
    pageList = soup.find_all(
        "a", attrs={"class": "page-navigator-number PNNW-S"})
    if len(pageList) > 0:
        pageSize = int(pageList[len(pageList) - 1].string)
        plist = getPageList(pageSize, art["path"][8:])
        list += plist
    return list


def getMusicDetail(music):
    url = "http://music.taihe.com" + music["path"]
    html = request(url)
    if html is None:
        print("detail is null : " + music["name"])
        return None
    soup = BeautifulSoup(html, features="lxml")
    special, publish, company = (None, None, None)

    tag = soup.find("p", class_="album desc")
    if tag is not None:
        special = tag.contents[1].string
    tag = soup.find("p", class_="publish desc")
    if tag is not None:
        publish = tag.string.replace("发行时间：", "")
    else:
        publish = "2019-01-01"
    tag = soup.find("p", class_="company desc")
    if tag is not None:
        company = tag.string.replace("发行公司：", "")

    lrc = ""
    tag = soup.find("div", id="lyricCont")
    if tag is not None:
        lrc = tag["data-lrclink"]
    detail = {}
    detail["special"] = special
    detail["publish"] = publish
    detail["company"] = company
    detail["lrc"] = lrc
    return detail


def getPageList(pageSize, id):
    list = []
    url = "http://music.taihe.com/data/user/getsongs?start={0}&size=15&ting_uid=" + str(
        id)
    while pageSize > 1:
        pageSize = pageSize - 1
        req = url.format(pageSize * 15)
        jsonText = request(req)
        if jsonText is None:
            print("pageList is null:" + str(pageSize) + "---id=" + str(id))
            continue
        obj = json.loads(jsonText)
        html = obj["data"]["html"]
        soup = BeautifulSoup(html, features="lxml")
        mlist = soup.find_all("a", attrs={"class": "namelink"})
        for music in mlist:
            dict = {"name": "", "path": ""}
            dict["name"] = music["title"]
            dict["path"] = music["href"]
            list.append(dict)
    return list


def capture(conn, musicList):
    musicList = getMusicList(art)
    if musicList is None:
        return
    for music in musicList:
        cursor.execute(
            'INSERT INTO tb_music(name) VALUES (?)', (music["name"],))
        musicId = cursor.lastrowid
        detail = getMusicDetail(music)
        if detail is not None:
            if detail["special"] is not None:
                cursor.execute(
                    'INSERT INTO tb_special(name) VALUES (?)', (detail["special"],))
                specialId = cursor.lastrowid
            else:
                specialId = 1

            if detail["company"] is not None:
                cursor.execute(
                    'INSERT INTO tb_company(name) VALUES (?)', (detail["company"],))
                companyId = cursor.lastrowid
            else:
                companyId = 1

            cursor.execute('INSERT INTO tb_release(musicId,singerId,specialId,companyId,lrc,publish) VALUES(?,?,?,?,?,?)', (
                musicId, singerId, specialId, companyId, detail["lrc"], detail["publish"],))
        conn.commit()
    conn.close()


def captureTest():
    i = 0
    artList = getArtList()
    for art in artList:
        i = i + 1
        if i < 5:
            continue
        print("----第" + str(i) + "位歌手：" + art["name"] + "-----")
        musicList = getMusicList(art)
        print(musicList)
        print("--------------musicList-------------")
        for music in musicList:
            print(music["name"] + " : ")
            detail = getMusicDetail(music)
            print(detail)
        if i >= 10:
            break


if __name__ == '__main__':
    pool = ThreadPool(3)
    conn = sqliteUtils.getConnection()
    cursor = conn.cursor()
    artList = getArtList()
    for art in artList:
        cursor.execute(
            'INSERT INTO tb_singer(name) VALUES (?)', (art["name"],))
        singerId = cursor.lastrowid
# print("--------------getArtList-----------------")
# artist = getArtList()
# print(artist[0])
# print("--------------getMusicList-----------------")
# mlist = getMusicList(artist[0])
# print(mlist)
# print("--------------getDetail-----------------")
# getMusicDetail(mlist[0])
